/* 
 * File:   net_client.h
 * Author: james
 *
 * Created on 2011年3月31日, 下午12:56
 */

#ifndef NET_CLIENT_H
#define	NET_CLIENT_H
#include <string>
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <boost/thread.hpp>
#include <boost/scoped_ptr.hpp>

#include "message.h"
#include "connection.h"

namespace edm
{
    namespace network
    {
        //! net client runing with a single thread
        class net_client_t: private boost::noncopyable
        {
        protected:
            net_client_t(const std::string& host, const std::string& port);
            ~net_client_t();
        public:
            //! start network client
            int start(callback_handler_t cb, callback_error_t cb_err, callback_connect_t cb_ok = 0);
            
            //! stop network client
            int stop();
            
            //! write message
            void async_write(COMMANDER command, edm_uint32_t reserved, std::string& body);
            //! write message
            void async_write(COMMANDER command, edm_uint32_t reserved, const char* body);
        protected:
            //! io service object of boost for deadline time and thread working
            boost::asio::io_service m_io_service;
        private:
            //! network client connect host
            std::string         m_host;
            //! network client connect port
            std::string         m_port;
            //! identify whether service is running
            bool m_started;
            //! callback_function_type when message event trigger
            callback_handler_t  m_callback_handler;
            //! callback_function_type when error event trigger
            callback_error_t    m_callback_error;
            //! callback_function_type when connect event trigger
            callback_connect_t  m_callback_connect;
            //! io service work object of boost for keeping io service is running
            boost::scoped_ptr<boost::asio::io_service::work> m_io_work;
            //! connection ptr for client
            connection_ptr_t m_client_connection; 
            //! thread pool
            boost::scoped_ptr<boost::thread>    m_thread;
        };
    
    }
}


#endif	/* NET_CLIENT_H */

